#include "ofApp.h"
#define SHARE_MEM_COLOR ("KinectColorData") 1920x1080x4
#define SHARE_MEM_DEPTH ("KinectDepthData") 512x424x2
#define SHARE_MEM_INFRARED ("KinectInfraredData") 512x424x2
#define SHARE_MEM_BODYINDEX ("KinectBodyIndexData") 512x424
#define SHARE_MEM_FRAMEINDEX ("FrameIndex") 4
//--------------------------------------------------------------
void ofApp::setup(){
	tmpImg.allocate(512,424,ofImageType::OF_IMAGE_COLOR);
	pData = new unsigned char[512 * 424 * 3];
	handle = getShareMemHandle(SHARE_MEM_DEPTH,512 * 424 * 2);
	handle1 = getShareMemHandle(SHARE_MEM_FRAMEINDEX,sizeof(int));
	oldIndex = 0;
}

//--------------------------------------------------------------
void ofApp::update(){
	int *nowIndex = (int *)getShareMemBuff(handle1);
	if(*nowIndex != oldIndex)
	{
		oldIndex = *nowIndex;
		unsigned short * p = (unsigned short *)getShareMemBuff(handle);
		for(int i = 0;i < 512 * 424;++ i)
		{
			pData[i * 3] = p[i] % 256;
			pData[i * 3 + 1] = (p[i] >> 8) % 256;
			pData[i * 3 + 2] = 0;
		}
		tmpImg.setFromPixels(pData,512,424,ofImageType::OF_IMAGE_COLOR);
	}
}
void ofApp::release()
{
	closeShareMemHandle(handle);
    closeShareMemHandle(handle1);
	delete [] pData;
}
//--------------------------------------------------------------
void ofApp::draw(){
	tmpImg.draw(0,0);
}

//--------------------------------------------------------------
void ofApp::keyPressed(int key){

}

//--------------------------------------------------------------
void ofApp::keyReleased(int key){

}

//--------------------------------------------------------------
void ofApp::mouseMoved(int x, int y ){

}

//--------------------------------------------------------------
void ofApp::mouseDragged(int x, int y, int button){

}

//--------------------------------------------------------------
void ofApp::mousePressed(int x, int y, int button){

}

//--------------------------------------------------------------
void ofApp::mouseReleased(int x, int y, int button){

}

//--------------------------------------------------------------
void ofApp::windowResized(int w, int h){

}

//--------------------------------------------------------------
void ofApp::gotMessage(ofMessage msg){

}

//--------------------------------------------------------------
void ofApp::dragEvent(ofDragInfo dragInfo){ 

}